package com.cloud.core.utils;

import android.content.Context;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.StyleSpan;

import com.cloud.widget.utils.DensityUtils;


/**
 * Created by Tanghaibo
 * 2018/3/28
 * description: 字符串掩码工具类
 */

public class RegexUtils {
    /**
     * 显示后3位,其他用*代替
     *
     * @param storageCode
     * @return
     */
    public static String formatStorageCode(String storageCode) {
        if (TextUtils.isEmpty(storageCode)) {
            return "";
        }
        if (storageCode.length() <= 3) {
            return storageCode;
        }
        StringBuffer format = new StringBuffer("\\S{");
        format.append(storageCode.length() - 3).append("}(\\S{3})");
        return storageCode.replaceAll(format.toString(), "**********$1");
    }

    /**
     * 加粗后四位字符
     *
     * @param text
     * @return
     */
    public static SpannableString formatBoldText(Context context, String text) {
        if (TextUtils.isEmpty(text)) {
            return new SpannableString("");
        }
        SpannableString spanString = new SpannableString(text);
        int index = text.lastIndexOf("/");
        int start = 0;
        if (index >= 4) {
            start = index - 4;
        }
        int startNext = index;
        if (text.length() - index >= 4) {
            startNext = text.length() - 4;
        }
        AbsoluteSizeSpan spanSize = new AbsoluteSizeSpan(DensityUtils.Dp2Px(context, 20));
        AbsoluteSizeSpan spanSizeStart = new AbsoluteSizeSpan(DensityUtils.Dp2Px(context, 20));
        spanString.setSpan(spanSizeStart, start, index, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        spanString.setSpan(spanSize, startNext, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        StyleSpan spanBold = new StyleSpan(Typeface.BOLD);
        StyleSpan spanBoldStart = new StyleSpan(Typeface.BOLD);
        spanString.setSpan(spanBoldStart, start, index, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanString.setSpan(spanBold, startNext, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanString;
    }

    /**
     * 加粗后四位字符
     *
     * @param text
     * @return
     */
    public static SpannableString boldText(Context context, String text) {
        if (TextUtils.isEmpty(text)) {
            return new SpannableString("");
        }
        SpannableString spanString = new SpannableString(text);
        int start = 0;
        if (text.length() >= 4) {
            start = text.length() - 4;
        }
        AbsoluteSizeSpan spanSize = new AbsoluteSizeSpan(DensityUtils.Dp2Px(context, 20));
        spanString.setSpan(spanSize, start, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        StyleSpan spanBold = new StyleSpan(Typeface.BOLD);
        spanString.setSpan(spanBold, start, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanString;
    }
}
